<template>
  <div>
    <el-card>
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="科目">
          <el-input
            v-model="searchForm.subject"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getData()">查询</el-button>
        </el-form-item>
        <el-form-item v-if="userInfo.type==='1' || userInfo.type==='2'">
            <!-- 新增 -->
            <el-button type="primary"><router-link to="testList/newTest"><span style="color:white">添加试题</span></router-link></el-button>
        </el-form-item>
      </el-form>
      <!-- 数据列表-->
      <el-table
        :data="tableData.slice((currentPage-1)*PageSize,currentPage*PageSize)"
        border
        class="table"
        ref="multipleTable"
        highlight-current-row
        
      >
        <el-table-column
          prop="subject"
          label="科目"
          min-width="60"
        ></el-table-column>
        <el-table-column
          prop="title"
          label="标题"
          min-width="120"
        ></el-table-column>
        <el-table-column
          prop="clazz"
          label="班级"
          min-width="100"
        >
          <template slot-scope="scope">
            {{ scope.row.clazz.grade }}年级{{ scope.row.clazz.classNumber }}班
          </template>
        </el-table-column>
        <el-table-column
          prop="teacher"
          label="出题人"
          min-width="80"
        >
            <template slot-scope="scope">
            {{ scope.row.teacher.name}}
          </template>
        </el-table-column>
        <el-table-column
          prop="totalScore"
          label="总分"
          min-width="40"
        ></el-table-column>
        <el-table-column
          prop="passScore"
          label="及格线"
          min-width="40"
        ></el-table-column>
        <el-table-column
          prop="examStart"
          label="答题时间"
          min-width="180"
        >
            <template slot-scope="scope">
            {{ scope.row.examStart}}至{{ scope.row.examEnd }}
          </template>
        </el-table-column>
        <el-table-column
          prop="limitTime"
          label="答题时长"
          min-width="40"
        ></el-table-column>
        <el-table-column
          label="操作"
          min-width="150"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button v-if="userInfo.type==='1' || userInfo.type==='2'" type="text"><router-link :to="{name: 'testContent', params: {id:scope.row.id}}">试卷内容</router-link></el-button>
            <el-button v-if="userInfo.type==='0'&&scope.row.isExam==='0'" type="primary"><router-link :to="{name: 'testQuestion', params: {id:scope.row.id,time:scope.row.limitTime}}" style="color:white">开始答题</router-link></el-button>
            <el-button
            v-if="userInfo.type==='1'|| userInfo.type==='2'"
              type="text"
              icon="el-icon-edit"
              @click="openDialog('编辑试卷', scope.row)"
              style="margin-right:5px"
              >编辑
            </el-button>
            <el-popconfirm
            v-if="userInfo.type==='1' || userInfo.type==='2'"
                confirm-button-text='确定'
                cancel-button-text='取消'
                icon="el-icon-info"
                @onConfirm="deleteUser(scope.row)"
                icon-color="red"
                title="确定删除该试卷吗？"
                >
                <el-button
              type="text"
              slot="reference"
              icon="el-icon-delete"
              class="red"
              >删除
            </el-button>
            </el-popconfirm>
            
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="pageSizes"
          :page-size="PageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.length"
        >
        </el-pagination>
      </div>
      <!--修改 模态框-->
      <el-dialog
        :title="dialogTitle"
        :visible.sync="dialogFormVisible"
        width="500px"
      >
        <el-form
          :model="editTestinfoForm"
          ref="editTestinfoForm"
          :rules="testInfoRules"
          label-width="120px"
          :inline="true"
        >
          <el-form-item label="标题:">
            <el-input v-model="editTestinfoForm.title" ></el-input>
          </el-form-item>
          <el-form-item label="科目:">
            <el-input v-model="editTestinfoForm.subject	" ></el-input>
          </el-form-item>
          <el-form-item label="出题人:">
            <el-select v-model="editTestinfoForm.teacher.name" placeholder="请选择出题人">
                <el-option
                v-for="item in teacherData"
                :key="item.id"
                :label="item.name"
                :value="item.accountNumber"
                ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="班级:">
            <el-select v-model="editTestinfoForm.clazz.id">
                <el-option
                v-for="item in classList"
                :key="item.id"
                :label="`${item.grade}年级${item.classNumber}班`"
                :value="item.id"
                ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="总分:">
            <el-input v-model="editTestinfoForm.totalScore" ></el-input>
          </el-form-item>
          <el-form-item label="及格线:">
            <el-input v-model="editTestinfoForm.passScore" ></el-input>
          </el-form-item>
          <el-form-item label="答题开始时间:">
            <el-input v-model="editTestinfoForm.examStart" ></el-input>
          </el-form-item>
          <el-form-item label="答题结束时间:">
            <el-input v-model="editTestinfoForm.examEnd" ></el-input>
          </el-form-item>
          <el-form-item label="答题时长:">
            <el-input v-model="editTestinfoForm.limitTime" ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button
            type="primary"
            @click="saveEdit"
            v-if="dialogTitle === '编辑试卷'"
            >保 存</el-button
          >
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import webservice from '../../../api/webservice'
export default {
  data() {
    return {
      tableData: [],
      // 默认显示第几页
        currentPage: 1,
        // 个数选择器（可修改）
        pageSizes: [10, 20,30,40,50],
        // 默认每页显示的条数（可修改）
        PageSize: 10,
      searchForm: {}, //查询对象
      dialogFormVisible: false, //添加或修改的模态框标识
      dialogTitle: "",
      editTestinfoForm: {
        teacher:{
          name:''
        },
        clazz:{
          id:''
        }
      }, //编辑人员信息的表单信息
      testInfoRules:{
          name: [
                { required: true, message: '请输入昵称', trigger: 'blur' },
            ],
      },
        questionList:[],//试题
        classList:[],
        userInfo:{},
        teacherData:[]
    };
  },
  mounted() {
    this.userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
    this.getData();
    this.getClassData()
    
    this.getUserData()
  },
  methods: {
    // 分页
    // 每页显示的条数
    handleSizeChange(val) {
        // 改变每页显示的条数 
        this.PageSize = val
        console.log(this.pageSizes)
        // 注意：在改变每页显示的条数时，要将页码显示到第一页
        this.currentPage = 1
    },
    // 显示第几页
    handleCurrentChange(val) {
        // 改变默认的页数
        this.currentPage = val
    },
    getData() {
      if(this.userInfo.type==='0'){
        var _param = {
        subject:this.searchForm.subject,
        studentId:this.userInfo.id
      };
      }else{
        var _param = {
        subject:this.searchForm.subject
      };
      }
      
       let _params = JSON.stringify(_param)
      webservice.HttpPost("/api/paper/selectPaperList", _params, (data) => {
        if (data.code === 200) {
          this.tableData = data.data;
          this.questionList = this.tableData.questionList
        } else {
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
    },
    //获取班级
        getClassData() {
      webservice.HttpPost("/api/clazz/selectClazzList", '{}', (data) => {
        if (data.code === 200) {
          this.classList = data.data;
        } else {
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
    },
    //获取人员信息
    getUserData() {
      let _param = {
        type:'1'
      };
       let _params = JSON.stringify(_param)
      webservice.HttpPost("/api/user/selectUserList", _params, (data) => {
        if (data.code === 200) {
          this.teacherData = data.data;
        } else {
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
    },
    //打开 编辑 模态框
    openDialog(flag, row) {
      this.dialogTitle = flag;
      if(flag==='编辑试卷'){
        this.editTestinfoForm = JSON.parse(JSON.stringify(row));
      }else{
        this.editTestinfoForm = {
          teacher:{
          name:''
        },
        clazz:{
          id:''
        }
        }
        
      }
      
      this.dialogFormVisible = true;
      
    },
    //修改
    saveEdit() {
      let _param = {
        id:this.editTestinfoForm.id,
        subject	:this.editTestinfoForm.subject,
        title:this.editTestinfoForm.title,
        teacherNumber:this.editTestinfoForm.teacher.name,
        totalScore:this.editTestinfoForm.totalScore,
        passScore:this.editTestinfoForm.passScore,
        clazzId:this.editTestinfoForm.clazz.id,
        examStart:this.editTestinfoForm.examStart,
        examEnd:this.editTestinfoForm.examEnd,
        limitTime:this.editTestinfoForm.limitTime,
      };
      let _params = JSON.stringify(_param)
      webservice.HttpPost("/api/paper/updatePaper", _params, (data) => {
        if (data.code === 200) {
          this.dialogFormVisible = false;
          this.getData()
          this.$message.success({
            message: '修改成功！',
            showClose: true,
            duration: 1000,
          });
        } else {
          this.dialogFormVisible = false;
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
    },
    //删除
    deleteUser(row){
      let _param = {
        id: row.id
      };
      let _params = JSON.stringify(_param)
      webservice.HttpPost("/api/paper/deletePaper", _params, (data) => {
        if (data.code === 200) {
            this.getData()
            this.$message.success({
            message: '删除成功！',
            showClose: true,
            duration: 1000,
          });
        } else {
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
    },
  },
};
</script>